package com.csyf.manager.service.impl;

import com.csyf.manager.entity.SysAgentChannel;
import com.csyf.manager.entity.SysPaymentChannel;
import com.csyf.manager.service.SysChannelBiz;
import com.csyf.manager.service.SysPaymentChannelBiz;
import com.csyf.manager.utils.HtmlUtils;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Enzo
 * @Date: 2018/3/29 10:13
 */
@Service
public class RedisService {


    private static final String order_notify_key = "order_notify:";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 交易通道
     */
    @Autowired
    private SysChannelBiz sysChannelBiz;

    /**
     * 代付通道
     */
    @Autowired
    private SysPaymentChannelBiz sysPaymentChannelBiz;


    private static String host = "http://www.frontwap.com/";

    private static String echost = "http://www.frontwap.com/";

    private static String huizhiHost = "http://www.frontwap.com/";

    /**
     * set stirng to redis
     *
     * @param key   key
     * @param value value
     * @param time  SECONDS
     */
    public void set(String key, String value, long time) {
        ValueOperations<String, String> forValue = stringRedisTemplate.opsForValue();
        forValue.set(key, value, time, TimeUnit.SECONDS);
    }


    public void deletekeys(String key) {
        Set<String> keys = stringRedisTemplate.keys(key + "*");
        stringRedisTemplate.delete(keys);
    }

    public void setnotifyUrl(String orderNo, String notifyurl) {
        ValueOperations<String, String> forValue = stringRedisTemplate.opsForValue();
        forValue.set(order_notify_key + orderNo, notifyurl, 24 * 60 * 60, TimeUnit.SECONDS);
    }

    public String getnotifyUrl(String orderNo) {
        ValueOperations<String, String> forValue = stringRedisTemplate.opsForValue();
        return forValue.get(order_notify_key + orderNo);
    }

    /**
     * 字符串操作
     * @param key
     * @param value
     */
    public void set(String key, String value) {
        ValueOperations<String, String> forValue = stringRedisTemplate.opsForValue();
        forValue.set(key, value);
    }

    /**
     * 字符串操作
     * @param key
     * @return
     */
    public String get(String key) {
        ValueOperations<String, String> forValue = stringRedisTemplate.opsForValue();
        return forValue.get(key);
    }

    /**
     * set操作
     * @param key
     * @param value
     */
    public void setSets(String key, String value) {
        stringRedisTemplate.opsForSet().add(key, value);
    }

    public boolean exist(String key) {
        return stringRedisTemplate.hasKey(key);
    }

    public void hset(String key, String field, String value) {
        HashOperations<String, Object, Object> hash = stringRedisTemplate.opsForHash();
        hash.put(key, field, value);
    }


    /**
     * 获取交易通道配置
     *
     * @param mch_id
     * @param src_mch_id
     * @return
     */
    public String getSrcChannelInfo(String mch_id, String src_mch_id) {
        String key = String.format("channel:%s:%s", mch_id, src_mch_id);
        if (!exist(key)) {
            SysAgentChannel sysAgentChannel = sysChannelBiz.queryByMchid(mch_id, src_mch_id);
            String toJson = new Gson().toJson(sysAgentChannel);
            set(key, toJson);
            return toJson;
        } else {
            return get(key);
        }
    }

    /**
     * 设置交易通道配置
     *
     * @param mch_id
     * @param src_mch_id
     * @param json
     */
    public void setSrcChannelInfo(String mch_id, String src_mch_id, String json) {
        String key = String.format("channel:%s:%s", mch_id, src_mch_id);
        set(key, json);
    }


    /**
     * 获取代付通道配置
     *
     * @param mch_id
     * @param src_mch_id
     * @return
     */
    public String getPaymentChannelInfo(String mch_id, String src_mch_id) {
        String key = String.format("paymentchannel:%s:%s", mch_id, src_mch_id);
        if (!exist(key)) {
            SysPaymentChannel sysPaymentChannel = sysPaymentChannelBiz.queryByMchid(mch_id, src_mch_id);
            String toJson = new Gson().toJson(sysPaymentChannel);
            set(key, toJson);
            return toJson;
        } else {
            return get(key);
        }
    }

    //=================================短信验证 START===========================================//

    /**
     * 设置代付通道配置
     *
     * @param mch_id
     * @param src_mch_id
     * @param json
     */
    public void setPaymentChannelInfo(String mch_id, String src_mch_id, String json) {
        String key = String.format("paymentchannel:%s:%s", mch_id, src_mch_id);
        set(key, json);
    }


    /**
     * 设置代付短信验证码
     *
     * @param phone
     * @param verifyCode
     */
    public void setPaymentSmsVerifyCode(String phone, String verifyCode) {
        String key = String.format("sms:payment:%s", phone);
        set(key, verifyCode, 300);
    }

    /**
     * 获取代付短信验证码
     *
     * @param phone
     * @return
     */
    public String getPaymentSmsVerifyCode(String phone) {
        String key = String.format("sms:payment:%s", phone);
        return get(key);
    }

    /**
     * 删除代付短信验证码
     *
     * @param phone
     */
    public void deletePaymentSmsVerifyCode(String phone) {
        String key = String.format("sms:payment:%s", phone);
        stringRedisTemplate.delete(key);
    }

    /**
     * 设置余额短信验证码
     *
     * @param phone
     * @param verifyCode
     */
    public void setBalanceSmsVerifyCode(String phone, String verifyCode) {
        String key = String.format("sms:balance:%s", phone);
        set(key, verifyCode, 300);
    }

    /**
     * 获取余额短信验证码
     *
     * @param phone
     * @return
     */
    public String getBalanceSmsVerifyCode(String phone) {
        String key = String.format("sms:balance:%s", phone);
        return get(key);
    }

    /**
     * 删除余额短信验证码
     *
     * @param phone
     */
    public void deleteBalanceSmsVerifyCode(String phone) {
        String key = String.format("sms:balance:%s", phone);
        stringRedisTemplate.delete(key);
    }

    /**
     * 设置商户登录 短信验证码
     *
     * @param phone
     * @param verifyCode
     */
    public void setLoginSmsVerifyCode(String phone, String verifyCode) {
        String key = String.format("sms:login:%s", phone);
        set(key, verifyCode, 300);
    }

    /**
     * 获取商户登录 短信验证码
     *
     * @param phone
     * @return
     */
    public String getLoginSmsVerifyCode(String phone) {
        String key = String.format("sms:login:%s", phone);
        return get(key);
    }

    /**
     * 删除商户登录 短信验证码
     *
     * @param phone
     */
    public void deleteLoginSmsVerifyCode(String phone) {
        String key = String.format("sms:login:%s", phone);
        stringRedisTemplate.delete(key);
    }


    private static final String bosslogin = "sms:bosslogin:%s";
    /**
     * 设置商户登录 短信验证码
     *
     * @param phone
     * @param verifyCode
     */
    public void setbossLoginSmsVerifyCode(String phone, String verifyCode) {
        String key = String.format(bosslogin, phone);
        set(key, verifyCode, 300);
    }

    /**
     * 获取商户登录 短信验证码
     *
     * @param phone
     * @return
     */
    public String getbossLoginSmsVerifyCode(String phone) {
        String key = String.format(bosslogin, phone);
        return get(key);
    }

    /**
     * 删除商户登录 短信验证码
     *
     * @param phone
     */
    public void deletebossLoginSmsVerifyCode(String phone) {
        String key = String.format(bosslogin, phone);
        stringRedisTemplate.delete(key);
    }

    //=================================短信验证 END===========================================//

    /**
     * 校验ip redis set 集合
     *
     * @param mch_id
     * @param ipaddress
     * @param suffix
     * @return
     */
    public boolean validateip(String mch_id, String ipaddress, String suffix) {
        SetOperations<String, String> setOperations = stringRedisTemplate.opsForSet();
        return setOperations.isMember(suffix + mch_id, ipaddress);
    }

    /**
     * p redis set 集合
     *
     * @param mch_id
     * @param ipaddress
     * @param suffix
     * @return
     */
    public void addSetValue(String mch_id, String ipaddress, String suffix) {
        SetOperations<String, String> setOperations = stringRedisTemplate.opsForSet();
        setOperations.add(suffix + mch_id, ipaddress);
    }

    /**
     * 校验银行卡号
     *
     * @param mch_id
     * @param bankCard
     * @param suffix
     * @return
     */
    public boolean validateBankcard(String mch_id, String bankCard, String suffix) {
        SetOperations<String, String> setOperations = stringRedisTemplate.opsForSet();
        return setOperations.isMember(suffix + mch_id, bankCard);
    }

    /**
     * redis保存post Form表单
     * @param url
     * @param map
     * @return
     */
    public String setform(String url, Map map) {
        String s = UUID.randomUUID().toString();
        String form = HtmlUtils.submitForm(url, map);

        set("pay:" + s, form, 2 * 60 * 60);
        return host + "openapi/transfer/pay/" + s;
    }

    public String setform(String host, String url, Map map) {
        String s = UUID.randomUUID().toString();
        String form = HtmlUtils.submitForm(url, map);

        set("pay:" + s, form, 2 * 60 * 60);
        return host + "openapi/transfer/pay/" + s;
    }


//    public String setGetform(String url, Map map) {
//        String s = UUID.randomUUID().toString();
//        String form = HtmlUtils.submitGetForm(url, map);
//
//        set("pay:" + s, form, 2 * 60 * 60);
//        return huizhiHost + "openapi/transfer/pay/" + s;
//    }

    public String setformString(String formStr) {
        String s = UUID.randomUUID().toString();
        String form = formStr;

        set("pay:" + s, form, 2 * 60 * 60);
        return host + "openapi/transfer/pay/" + s;
    }

    /**
     * 引导页面
     * 页面跳转
     * @param formStr
     * @return
     */
    public String setHtmlString(String formStr,String orderjson) {
        String s = UUID.randomUUID().toString();
        String form = formStr;

        set("topay:" + s, form, 2 * 60 * 60);
        set("payorder:" + s, orderjson, 2 * 60 * 60);
        return echost + "openapi/topay/" + s;
    }


    /**
     * 获取绑卡ID
     *
     * @param outTradeNo
     * @return
     */
    public String getBindId(String outTradeNo) {
        String key = String.format("yisi_fast:bindid:%s", outTradeNo);
        return get(key);
    }

    /**
     * 设置绑卡ID
     *
     * @param outTradeNo
     */
    public void setBindId(String outTradeNo, String bindId) {
        set("yisi_fast:bindid:" + outTradeNo, bindId, 24 * 60 * 60);
    }


    //=================================python 跳转===========================================//

    /**
     * 表单提交跳转
     * @param formStr
     * @return
     */
    public String setPyformString(String formStr) {
        String s = UUID.randomUUID().toString();
        String form = formStr;

        set("pay:" + s, form, 2 * 60 * 60);
        return host + "openapi/pay/" + s;
    }


}
